﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;


namespace PostSharp4ViewState.InstallUtils
{
   [RunInstaller(true)]
   public partial class InstallPlugin : Installer
   {
      private static readonly string[] files = new string[] {
         "PostSharp4ViewState.psplugin",
         "PostSharp4ViewState.dll", 
         "PostSharp4ViewState.Weaver.dll"};

      public InstallPlugin()
      {
         InitializeComponent();
      }
      public override void Install(IDictionary stateSaver)
      {
         base.Install(stateSaver);
         string Key =
            (string)
            Microsoft.Win32.Registry.GetValue(
               "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\.NETFramework\\AssemblyFolders\\PostSharp 1.0", null,
               null);
         if (Key == null)
         {
            throw new InvalidOperationException("PostSharp 1.0 not installed.");
         }
         string path = Path.Combine(Key, "PlugIns");
         if (!Directory.Exists(path))
         {
            throw new InvalidOperationException("PostSharp 1.0 plug-in folder does not exist.");
         }
         string appPath = Context.Parameters["DP_TargetDir"];
         foreach (string file in files)
         {
            File.Copy(Path.Combine(appPath, file), Path.Combine(path, file), true);
         }         
      }
      public override void Uninstall(IDictionary savedState)
      {
         base.Uninstall(savedState);
         string Key =
            (string)
            Microsoft.Win32.Registry.GetValue(
               "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\.NETFramework\\AssemblyFolders\\PostSharp 1.0", null,
               null);
         if (Key != null)
         {

            string path = Path.Combine(Key, "PlugIns");
            if (Directory.Exists(path))
            {               
               foreach (string file in files)
               {
                  File.Delete(Path.Combine(path, file));
               }
            }
         }
      }
   }
}
